package com.ruoyi.teaching.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.teaching.domain.YaStudent;
import com.ruoyi.teaching.domain.YaTeachingPlan;
import com.ruoyi.teaching.service.IYaStudentService;
import com.ruoyi.teaching.service.IYaTeachingPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaDailyTasksMapper;
import com.ruoyi.teaching.domain.YaDailyTasks;
import com.ruoyi.teaching.service.IYaDailyTasksService;

/**
 * 每日任务Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-17
 */
@Service
public class YaDailyTasksServiceImpl implements IYaDailyTasksService {
  @Autowired
  private YaDailyTasksMapper yaDailyTasksMapper;

  /**
   * 查询每日任务
   *
   * @param id 每日任务主键
   * @return 每日任务
   */
  @Override
  public YaDailyTasks selectYaDailyTasksById(Long id) {
    return yaDailyTasksMapper.selectYaDailyTasksById(id);
  }

  /**
   * 查询每日任务列表
   *
   * @param yaDailyTasks 每日任务
   * @return 每日任务
   */
  @Override
  public List<YaDailyTasks> selectYaDailyTasksList(YaDailyTasks yaDailyTasks) {
    return yaDailyTasksMapper.selectYaDailyTasksList(yaDailyTasks);
  }

  /**
   * 新增每日任务
   *
   * @param yaDailyTasks 每日任务
   * @return 结果
   */
  @Override
  public int insertYaDailyTasks(YaDailyTasks yaDailyTasks) {
    yaDailyTasks.setCreateTime(DateUtils.getNowDate());
    return yaDailyTasksMapper.insertYaDailyTasks(yaDailyTasks);
  }

  /**
   * 修改每日任务
   *
   * @param yaDailyTasks 每日任务
   * @return 结果
   */
  @Override
  public int updateYaDailyTasks(YaDailyTasks yaDailyTasks) {
    yaDailyTasks.setUpdateTime(DateUtils.getNowDate());
    return yaDailyTasksMapper.updateYaDailyTasks(yaDailyTasks);
  }

  /**
   * 批量删除每日任务
   *
   * @param ids 需要删除的每日任务主键
   * @return 结果
   */
  @Override
  public int deleteYaDailyTasksByIds(Long[] ids) {
    return yaDailyTasksMapper.deleteYaDailyTasksByIds(ids);
  }

  /**
   * 删除每日任务信息
   *
   * @param id 每日任务主键
   * @return 结果
   */
  @Override
  public int deleteYaDailyTasksById(Long id) {
    return yaDailyTasksMapper.deleteYaDailyTasksById(id);
  }

  @Autowired
  private IYaTeachingPlanService iYaTeachingPlanService;
  @Autowired
  private IYaStudentService iYaStudentService;

  /**
   * 发布每日任务
   *
   * @param msg
   */
  @Override
  public void publishDailyTask(String msg) {
//    获取到今天所有班级的教学计划
    YaTeachingPlan plan = new YaTeachingPlan();
    plan.setDate(msg);
    List<YaTeachingPlan> list = iYaTeachingPlanService.selectYaTeachingPlanList(plan);
    List<YaDailyTasks> dailyTasks = new ArrayList<>();

//    遍历教学计划
    for (YaTeachingPlan yaTeachingPlan : list) {
      String content = yaTeachingPlan.getCourseContent();
      if ("自习".equals(content) || "休息".equals(content)) {
        continue;
      }
      Long classesId = yaTeachingPlan.getClassesId();
      YaStudent student = new YaStudent();
      student.setClassesId(classesId);
      List<YaStudent> students = iYaStudentService.selectYaStudentList(student);

      //    为每个班的每个学生生成每日任务
      for (YaStudent yaStudent : students) {
        YaDailyTasks queryTask = new YaDailyTasks();
        queryTask.setDate(msg);
        queryTask.setClassesId(classesId);
        queryTask.setStudentId(yaStudent.getId());
        List<YaDailyTasks> tasks = yaDailyTasksMapper.selectYaDailyTasksList(queryTask);
        if (!tasks.isEmpty()) {
          continue;
        }

        YaDailyTasks dailyTask = new YaDailyTasks();
        dailyTask.setDate(msg);
        dailyTask.setCourseContent(content);
        dailyTask.setStage(yaTeachingPlan.getStage());
        dailyTask.setClassesId(classesId);
        dailyTask.setStudentId(yaStudent.getId());
        dailyTask.setStatus(2);
        dailyTasks.add(dailyTask);
      }
    }

//    批量插入每日任务
    if (!dailyTasks.isEmpty()) {
      yaDailyTasksMapper.insertBatchYaDailyTasks(dailyTasks);
    }
  }

  /**
   * 检查每日任务
   *
   * @param msg
   */
  @Override
  public void checkDailyTask(String msg) {
    YaDailyTasks yaDailyTasks = new YaDailyTasks();
    yaDailyTasks.setDate(msg);
    List<YaDailyTasks> list = yaDailyTasksMapper.selectYaDailyTasksList(yaDailyTasks);
//    List<YaDailyTasks> dailyTasksList = new ArrayList<>();
    for (YaDailyTasks dailyTask : list) {
//      已经提交并检查了的
      if (3 == dailyTask.getStatus()&& null != dailyTask.getQualified()){
        continue;
      }
//      没有检查的
      //    将msg日期 未检查的任务和状态为已经提交的 设置为合格
      if (3 == dailyTask.getStatus()&& null == dailyTask.getQualified()){
        dailyTask.setQualified(1);
      }
      //    将msg日期 未提交的任务设置为不及格，状态设置为已过期
      if (2 == dailyTask.getStatus()&& null == dailyTask.getQualified()){
        dailyTask.setQualified(2);
      }
      dailyTask.setStatus(4);
      yaDailyTasksMapper.updateYaDailyTasks(dailyTask);
//      dailyTasksList.add(dailyTask);
    }

//    批量修改
/*    if (!dailyTasksList.isEmpty()) {
      yaDailyTasksMapper.updateBatchYaDailyTasks(dailyTasksList);
    }*/
  }
}
